<template>
    <div>
         <ul class="nav nav-tabs" role="tablist">
            <li role="presentation" class="active"><a href="#xjlyd" role="tab" data-toggle="tab">新建领用单</a></li>
            <li role="preaentation"><a href="#lyjl" role="tab" data-toggle="tab">领用记录</a></li>
        </ul>
        <div class="tab-content">
            <div role="tabpanel" class="tab-pane  active" id="xjlyd">
                <table id="store_table"></table>
                <div class="modal fade" id="pickup_add_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="exampleModalLabel">新建领用单</h4>
                            </div>
                            <div class="modal-body">
                                <form>
                                    <div class="form-group">
                                        <label for="pickup_add_qty" class="control-label">领用数量：</label>
                                        <div class="input-group">
                                            <input type="number" min="1" class="form-control" id="pickup_add_qty">
                                            <span id="pickup_add_storenum" class="input-group-addon">库存：<span><b></b></span>
                                        </div>
                                        <span id="tips" class="hidden" style="color:#f00">领用数量超出库存数量</span>
                                    </div>

                                    <div class="form-group">
                                        <label for="pickup_add_ftype1" class="control-label">备注(用途)：</label>
                                        <textarea rows="3" id="fnotes" class="form-control"></textarea>
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                <button type="button" class="btn btn-primary" @click="addPickup">提交</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div role="tabpanel" class="tab-pane" id="lyjl">
                <table id="pickuprecord_table"></table>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                currentRow: ''
            }
        },
        methods: {
            getNotPickup() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/instock/fetchstore',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        var operateEvents = {
                            'click a[name="pickup"]': function(e, value, row, index) {
                                _this.currentRow = row
                                _this.showAddPickupModal()
                            }
                        }
                        $('#store_table').bootstrapTable('destroy')
                        $('#store_table').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                radio: true
                            }, {
                                field: 'fitemno',
                                title: '物料号',
                                halign: 'center'
                            }, {
                                field: 'fcgn',
                                title: '物料名称',
                                halign: 'center'
                            }, {
                                field: 'fmodel',
                                title: '型号',
                                halign: 'center'
                            }, {
                                field: 'fqty',
                                title: '库存数量',
                                halign: 'center'
                            }, {
                                field: 'operate',
                                title: '操作',
                                halign: 'center',
                                formatter: '<a class="btn btn-success btn-xs" name="pickup">领用</a>',
                                events: operateEvents
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            selectItemName: 'store',
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            maintainSelected: true,
                            // showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: false,
                            // radio: true,
                            checkbox: true,
                        });
                    }
                });
            },
            getPickup() {
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/instock/fetchallpickup',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#pickuprecord_table').bootstrapTable('destroy')
                        $('#pickuprecord_table').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                field: 'fidno',
                                title: '领用单号',
                                halign: 'center'
                            }, {
                                field: 'fitemno',
                                title: '物料号',
                                halign: 'center'
                            }, {
                                field: 'fcgn',
                                title: '物料名称',
                                halign: 'center'
                            }, {
                                field: 'fmodel',
                                title: '型号',
                                halign: 'center'
                            }, {
                                field: 'fprice',
                                title: '单价',
                                halign: 'center',
                                visible: false
                            }, {
                                field: 'fqty',
                                title: '数量',
                                halign: 'center'
                            }, {
                                field: 'fnotes',
                                title: '领用备注',
                                halign: 'center'
                            }, {
                                field: 'entuser',
                                title: '领用人',
                                halign: 'center'
                            }, {
                                field: 'entdate',
                                title: '领用日期',
                                halign: 'center'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            selectItemName: 'material',
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            maintainSelected: true,
                            //showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            // clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: false,
                            // radio: true,
                            checkbox: true,
                        });
                    }
                });
            },
            showAddPickupModal() {
                $('#pickup_add_modal').modal('show')
                $('#pickup_add_storenum b').text(this.currentRow.fqty)
            },
            addPickup() {
                var _this = this
                if (parseInt($('#pickup_add_qty').val()) > parseInt($('#pickup_add_storenum b').text())) {
                    $('#tips').removeClass('hidden');
                    $('#pickup_add_submit').blur(function() {
                        $('#tips').addClass('hidden');
                    })
                } else {
                    $.ajax({
                        type: 'POST',
                        url: 'http://192.168.1.66:808/api/instock/add',
                        data: {
                            'token': localStorage.token,
                            'userid': localStorage.userid,
                            'fitemno': _this.currentRow.fitemno,
                            'fmodel': _this.currentRow.fmodel,
                            'fcgn': _this.currentRow.fcgn,
                            'fqty': -parseInt($('#pickup_add_qty').val()),
                            'ftype': false,
                            'fnotes': $('#fnotes').val()
                        },
                        dataType: 'json',
                        async: false,
                        crossDomain: true,
                        error: function(request) {
                            _this.$store.commit('showModal','连接错误')
                        },
                        success: function(retdata) {
                            $('#pickup_add_modal').modal('hide');
                            _this.getNotPickup()
                            _this.getPickup()
                        }
                    });
                }
            }
        },
        mounted() {
            this.getNotPickup()
            this.getPickup()
        }
    }
</script>
<style>

</style>